package com.ruoyi.manage.enterprise.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseCheck;
import com.ruoyi.manage.enterprise.domain.LfQyjbxx;
import com.ruoyi.manage.enterprise.domain.vo.EcEnterpriseCheckVo;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseCheckService;
import com.ruoyi.manage.enterprise.service.ILfQyjbxxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 企业注册核验Controller
 *
 * @author ruoyi
 * @date 2025-08-15
 */
@Api(tags="企业注册核验控制器")
@RestController
@RequestMapping("/manage/check" )
public class EcEnterpriseCheckController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IEcEnterpriseCheckService ecEnterpriseCheckService;
    @Autowired
    private ILfQyjbxxService iLfQyjbxxService;

    /**
     * 查询企业注册核验列表
     */
    @ApiOperation(value = "查询企业注册核验",notes = "企业注册核验管理")
    @PreAuthorize("@ss.hasPermi('manage:check:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(EcEnterpriseCheckVo ecEnterpriseCheck,QueryWrapper<EcEnterpriseCheck> queryWrapper) {
        setQueryCondition(ecEnterpriseCheck,queryWrapper);
        startPage();
        List<EcEnterpriseCheck> list = ecEnterpriseCheckService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(EcEnterpriseCheckVo ecEnterpriseCheck, QueryWrapper<EcEnterpriseCheck> queryWrapper) {
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getCreditCode())){
            queryWrapper.eq(TABLE_PREFIX+"credit_code" ,ecEnterpriseCheck.getCreditCode());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getCompanyName())){
            queryWrapper.like(TABLE_PREFIX+"company_name" ,ecEnterpriseCheck.getCompanyName());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getLocalCode())){
            queryWrapper.eq(TABLE_PREFIX+"local_code" ,ecEnterpriseCheck.getLocalCode());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getRegistrationFlag())){
            queryWrapper.eq(TABLE_PREFIX+"registration_flag" ,ecEnterpriseCheck.getRegistrationFlag());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getLocalEnterprise())){
            queryWrapper.eq(TABLE_PREFIX+"local_enterprise" ,ecEnterpriseCheck.getLocalEnterprise());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getBlackListLocal())){
            queryWrapper.eq(TABLE_PREFIX+"black_list_local" ,ecEnterpriseCheck.getBlackListLocal());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getBlackListCountry())){
            queryWrapper.eq(TABLE_PREFIX+"black_list_country" ,ecEnterpriseCheck.getBlackListCountry());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getLocalAdministrativePenalties())){
            queryWrapper.eq(TABLE_PREFIX+"local_administrative_penalties" ,ecEnterpriseCheck.getLocalAdministrativePenalties());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getCountryAdministrativePenalties())){
            queryWrapper.eq(TABLE_PREFIX+"country_administrative_penalties" ,ecEnterpriseCheck.getCountryAdministrativePenalties());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getVersion())){
            queryWrapper.eq(TABLE_PREFIX+"version" ,ecEnterpriseCheck.getVersion());
        }
        if(ecEnterpriseCheck.getDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"deleted" ,ecEnterpriseCheck.getDeleted());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getProvinceName())){
            queryWrapper.like(TABLE_PREFIX+"province_name" ,ecEnterpriseCheck.getProvinceName());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getCityName())){
            queryWrapper.like(TABLE_PREFIX+"city_name" ,ecEnterpriseCheck.getCityName());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getAreaAdress())){
            queryWrapper.eq(TABLE_PREFIX+"area_adress" ,ecEnterpriseCheck.getAreaAdress());
        }
        if(ecEnterpriseCheck.getSiteId() != null){
            queryWrapper.eq(TABLE_PREFIX+"site_id" ,ecEnterpriseCheck.getSiteId());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getLocalName())){
            queryWrapper.like(TABLE_PREFIX+"local_name" ,ecEnterpriseCheck.getLocalName());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getFullCode())){
            queryWrapper.eq(TABLE_PREFIX+"full_code" ,ecEnterpriseCheck.getFullCode());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getProvinceCode())){
            queryWrapper.eq(TABLE_PREFIX+"province_code" ,ecEnterpriseCheck.getProvinceCode());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getCityCode())){
            queryWrapper.eq(TABLE_PREFIX+"city_code" ,ecEnterpriseCheck.getCityCode());
        }
        if(StrUtil.isNotBlank(ecEnterpriseCheck.getAreaCode())){
            queryWrapper.eq(TABLE_PREFIX+"area_code" ,ecEnterpriseCheck.getAreaCode());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    @GetMapping("/enterprise")
    @Anonymous
    public AjaxResult checkEnterprise(String enterpriseName){
        List<LfQyjbxx> list = iLfQyjbxxService.lambdaQuery().like(LfQyjbxx::getQymc, enterpriseName).list();
        return AjaxResult.success(list);
    }

    /**
     * 导出企业注册核验列表
     */
    @PreAuthorize("@ss.hasPermi('manage:check:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, EcEnterpriseCheckVo ecEnterpriseCheck,QueryWrapper<EcEnterpriseCheck> queryWrapper) {
        if (CollUtil.isEmpty( ecEnterpriseCheck.getIds())){
            setQueryCondition(ecEnterpriseCheck,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",ecEnterpriseCheck.getIds());
        }
        List<EcEnterpriseCheck> list = ecEnterpriseCheckService.list(queryWrapper);
        ExcelUtil<EcEnterpriseCheck> util = new ExcelUtil<EcEnterpriseCheck>(EcEnterpriseCheck. class);
        util.exportExcel(response,list, "check");
    }

    /**
     * 获取企业注册核验详细信息
     */
    @ApiOperation(value = "获取企业注册核验",notes = "企业注册核验管理")
    @PreAuthorize("@ss.hasPermi('manage:check:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(ecEnterpriseCheckService.queryGetById(id));
    }

    /**
     * 新增企业注册核验
     */
    @ApiOperation(value = "新增企业注册核验",notes = "企业注册核验管理")
    @PreAuthorize("@ss.hasPermi('manage:check:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EcEnterpriseCheck ecEnterpriseCheck) {
        return toAjax(ecEnterpriseCheckService.addEcEnterpriseCheck(ecEnterpriseCheck) ? 1 : 0);
    }

    /**
     * 修改企业注册核验
     */
    @ApiOperation(value = "修改企业注册核验",notes = "企业注册核验管理")
    @PreAuthorize("@ss.hasPermi('manage:check:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody EcEnterpriseCheck ecEnterpriseCheck) {
        return toAjax(ecEnterpriseCheckService.updateEcEnterpriseCheck(ecEnterpriseCheck) ? 1 : 0);
    }

    /**
     * 删除企业注册核验
     */
    @ApiOperation(value = "删除企业注册核验",notes = "企业注册核验管理")
    @PreAuthorize("@ss.hasPermi('manage:check:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ecEnterpriseCheckService.deleteEcEnterpriseCheckByIds(ids) ? 1 : 0);
    }
}
